package cn.itcast.web.controller.cargo;


import cn.itcast.common.utils.BeanMapUtils;
import cn.itcast.domain.cargo.*;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.*;
import cn.itcast.vo.ContractProductVo;
import cn.itcast.vo.FinanceVo;
import cn.itcast.vo.InvoiceVo;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

//财务报运单管理
@Controller
@RequestMapping("/cargo/finance")
public class FinanceController extends BaseController {

    @Reference
    private FinanceService financeService;

    @Reference
    private InvoiceService invoiceService;

    @Reference
    private PackingService packingService;

    @Reference
    private ExportService exportService;

    @Reference
    private ExportProductService exportProductService;

    //分页管理
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "5") int size) {
        //1、根据财务报运单的id，查询所有的数据
        PageInfo info = financeService.findAll(page, size, null);
        request.setAttribute("page", info);
        return "cargo/finance/finance-list";
    }


    //进入到新增页面
    @RequestMapping("/toAdd")
    public String toAdd() {
        InvoiceExample example = new InvoiceExample();
        InvoiceExample.Criteria criteria = example.createCriteria();
        criteria.andStateEqualTo(0);
        List<Invoice> list=invoiceService.findAll(example);
        request.setAttribute("invoiceList",list);
        return "cargo/finance/finance-add";
    }


    @RequestMapping(path = "/edit.do")
    public String edit(Finance finance) {
        System.out.println(finance);
        financeService.save(finance);
        //重定向到列表
        return  "redirect:/cargo/finance/list.do";
    }

    //打印报表
    @RequestMapping("/printExcel")
    public void printExcel(String id) throws IOException {
        System.out.println(id);
        //1.准备数据
        Packing packing = packingService.findById(id);
        System.out.println(packing == null);
        String[] exportIds = packing.getExportIds().split(",");
        ExportExample example = new ExportExample();
        ExportExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(Arrays.asList(exportIds));
        List<Export> exports = exportService.findAll(example);
        Map<String, Object> map = BeanMapUtils.beanToMap(exports.get(0));
        map.put("inputDate","2020-5-13");
        System.out.println(map);
        List list = new ArrayList();
        for (int i = 0; i < exports.size(); i++) {
            map.put("invoiceNo", id);
            ExportProductExample epe = new ExportProductExample();
            ExportProductExample.Criteria epeCriteria = epe.createCriteria();
            epeCriteria.andExportIdEqualTo(exports.get(i).getId());
            list.addAll(exportProductService.findAll(epe));
        }
            //2.设置下载信息
            response.setContentType("application/vnd.ms-excel"); //下载excel
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("财务单", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            //3.加载excel模板
            String path = session.getServletContext().getRealPath("/")+"/make/finance.xlsx";
            //4.创建EasyExcel的excelWtire对象( 用于数据填充)
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
                    .head(ContractProductVo.class) //设置表头
                    .withTemplate(path) //加载模板
                    .build();
            //获取sheet对象
            WriteSheet sheet = EasyExcel.writerSheet().build();
            //5.调用方法完成填充map数据
            excelWriter.fill(map, sheet);
            //6.调用方法完成填充list数据
            excelWriter.fill(list, sheet);
            //7.属性资源，完成下载
            excelWriter.finish(); //下载excel文件，释放内存资源

    }

}
